package com.canbot.u05.activity;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.adapter.HoldListViewAdapter;
import com.canbot.u05.utils.SPUtils;
import com.canbot.u05.utils.ToastTools;

import java.util.List;

/**
 * 语音生成器的保存
 */
public class HoldTtsActivity extends BaseActivityNoTitle implements AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener {

        private HoldListViewAdapter holdListViewAdapter;

        private ListView listView;

        private PopupWindow popupWindow;

        private List<String> stringList;

        private String date;

        private Context mContext;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_hold_tts);
                mContext=this;
                listView = (ListView) findViewById(R.id.listView_hold);

                date = getIntent().getStringExtra("data");
                holdListViewAdapter = new HoldListViewAdapter(mContext, getData());
                listView.setAdapter(holdListViewAdapter);
                holdListViewAdapter.notifyDataSetChanged();
                listView.setOnItemLongClickListener(this);
                listView.setOnItemClickListener(this);
        }

        /**
         * 返回键
         */
        public void speechbackchoose(View view) {
                finish();
        }

        /**
         * 获取数据
         */
        public List<String> getData() {
                stringList = JSON.parseArray(date, String.class);
                return stringList;
        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(mContext, SpeechGenerationActivity.class);
                intent.putExtra("text", stringList.get(i));
                setResult(RESULT_OK,intent);
                finish();
        }

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                showPopupWindow(view, i);
                return true;
        }

        //弹框的显示
        public void showPopupWindow(View view, final int position) {
                View contentView;
                // 一个自定义的布局，作为显示的内容
                String text = stringList.get(position);
                ToastTools.newInstance(this).showText("-----" + text.toString().length());
                if (text.toString().length() < 16) {
                        contentView = LayoutInflater.from(mContext).inflate(R.layout.hold_tts_item_dan, null);
                }
                else {
                        contentView = LayoutInflater.from(mContext).inflate(R.layout.hold_tts_item_shuang, null);
                }

                // 设置按钮的点击事件
                TextView textView = (TextView) contentView.findViewById(R.id.text_dan);
                Button button = (Button) contentView.findViewById(R.id.btn_delete_dan);
                textView.setText(text);

                button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                stringList.remove(position);
                                String json = JSON.toJSONString(stringList);
                                SPUtils.saveString(mContext, "Commonlanguage", json);
                                popupWindow.dismiss();
                                holdListViewAdapter.notifyDataSetChanged();
                        }
                });

                popupWindow = new PopupWindow(contentView,
                        ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);


                popupWindow.setTouchInterceptor(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                Log.i("mengdd", "onTouch : ");
                                return false;
                                // 这里如果返回true的话，touch事件将被拦截
                                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

                        }
                });

                // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
                if (text.toString().length() < 16) {
                        popupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(
                                R.drawable.pop_dan));
                }
                else {
                        popupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(
                                R.drawable.pop_shuang));
                }


                // 设置好参数之后再show    location[0]水平 -左
                //popupWindow.showAsDropDown(view);

                int[] location = new int[2];
                view.getLocationOnScreen(location);

                if (text.toString().length() < 16) {
                        popupWindow.showAsDropDown(view,  0, -(int)mContext.getResources().getDimension(R.dimen.x98));
                }
                else {
                        popupWindow.showAsDropDown(view, 0, -(int)mContext.getResources().getDimension(R.dimen.x120));
                }

        }


}
